//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 12
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using ProxyHelpers.EWS;

namespace DeleteAttachmentAndGetItemWithBug
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential("username", "password", "domain");

            // You must have a valid attachment ItemId in order to call DeleteAttachmentAndGetItemWithBug

        }

        /// <summary>
        /// Deletes an attachment and returned the Id and updated change key of the 
        /// parent item (Listing 12-15)
        /// </summary>
        /// <param name="binding">Exchange binding to use</param>
        /// <param name="attachmentId">Attachment id to delete</param>
        /// <returns>Id and updated change key of the parent item</returns>
        /// 
        public static ItemIdType DeleteAttachmentAndGetItemWithBug(
                         ExchangeServiceBinding binding,
                         RequestAttachmentIdType attachmentId)
        {
            DeleteAttachmentType request = new DeleteAttachmentType();
            request.AttachmentIds = new RequestAttachmentIdType[] { attachmentId };

            DeleteAttachmentResponseType response =
                        binding.DeleteAttachment(request);
            DeleteAttachmentResponseMessageType responseMessage =
                        response.ResponseMessages.Items[0] as
                              DeleteAttachmentResponseMessageType;
            if (responseMessage.ResponseCode != ResponseCodeType.NoError)
            {
                throw new Exception("Delete attachment failed.  Response code: " +
                           responseMessage.ResponseCode.ToString());
            }
            else
            {
                // Normally, we could just return the root item id from our Delete 
                // Attachment response.  But I want to prove a point here, so we will 
                // call GetItem and see what happens.

                // There is a bug here....
                //
                GetItemType getRequest = new GetItemType();
                getRequest.ItemIds = new BaseItemIdType[] { 
                          responseMessage.RootItemId };
                getRequest.ItemShape = new ItemResponseShapeType(
                                      DefaultShapeNamesType.IdOnly);
                GetItemResponseType getResponse = binding.GetItem(getRequest);
                ItemInfoResponseMessageType getResponseMessage =
                         getResponse.ResponseMessages.Items[0] as
                                       ItemInfoResponseMessageType;

                if (getResponseMessage.ResponseCode != ResponseCodeType.NoError)
                {
                    throw new Exception("GetItem failed.  Response code: " +
                             getResponseMessage.ResponseCode.ToString());
                }
                else
                {
                    return getResponseMessage.Items.Items[0].ItemId;
                }
            }
        }

    }
}
